/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CriarAtleta.java
 *
 * Created on 01/05/2011, 04:20:15
 */
package Interface;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import ludi.Atleta;
import ludi.Curso;
import ludi.Modalidade;
import ludi.Time;
import ludi.Usuario;
import ludiExcecoes.AtletaExcecao;
import ludiExcecoes.GUILudi;
import utils.BancoDeDados;

/**
 *
 * @author alison
 */
public class CriarAtleta extends javax.swing.JInternalFrame {

    BancoDeDados banco;
    Usuario usuario;
    Atleta editAtleta;
    ArrayList<String> listaDeCursos;

    /** Creates new form CriarAtleta */
    public CriarAtleta(BancoDeDados banco, Usuario usuario, Atleta editAtleta) {
        try {
            this.banco = banco;
            this.usuario = usuario;
            this.editAtleta = editAtleta;
            this.listaDeCursos = banco.getListaDeCursos();
            initComponents();
            if (editAtleta == null) {
                radioMasculino.setSelected(true);
                botaoAlterar.setEnabled(false);
                botaoExcluir.setEnabled(false);
                painelAbas.setEnabledAt(1, false);
                if (usuario.getPermissoes() == 2) {
                    seletorCurso.setSelectedIndex(listaDeCursos.indexOf(usuario.getLogin()));
                    seletorCurso.setEnabled(false);
                }
            } else {
                campoNome.setText(editAtleta.getNome());
                campoMatricula.setText(editAtleta.getMatricula());
                campoRG.setText(editAtleta.getRg());
                if (editAtleta.getGenero() == 1) {
                    radioMasculino.setSelected(true);
                } else {
                    radioFeminino.setSelected(true);
                }
                seletorCurso.setSelectedIndex(listaDeCursos.indexOf(editAtleta.getCurso()));
                if (usuario.getPermissoes() == 2){
                    seletorCurso.setEnabled(false);
                }
                botaoCadastrar.setEnabled(false);
            }
            setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(CriarAtleta.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        painelAbas = new javax.swing.JTabbedPane();
        abaDados = new javax.swing.JPanel();
        labelMatricula = new javax.swing.JLabel();
        campoMatricula = new javax.swing.JTextField();
        labelRG = new javax.swing.JLabel();
        campoRG = new javax.swing.JTextField();
        labelGenero = new javax.swing.JLabel();
        radioMasculino = new javax.swing.JRadioButton();
        radioFeminino = new javax.swing.JRadioButton();
        labelCurso = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        labelNome = new javax.swing.JLabel();
        seletorCurso = new javax.swing.JComboBox();

        ArrayList<String> nomesDosCursos = new ArrayList<String>(0);

        for (int i = 0; i < listaDeCursos.size(); i++){
            try {
                Curso curso = banco.getCurso(listaDeCursos.get(i));
                nomesDosCursos.add(curso.getNome() + " - " + curso.getLogin());
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        seletorCurso.setModel(new javax.swing.DefaultComboBoxModel(nomesDosCursos.toArray()));
        abaTimes = new javax.swing.JPanel();
        scrollTabelaTimes = new javax.swing.JScrollPane();
        tabelaTimes = new javax.swing.JTable();
        botaoGerenciar = new javax.swing.JButton();
        botaoAlterar = new javax.swing.JButton();
        botaoExcluir = new javax.swing.JButton();
        botaoCancelar = new javax.swing.JButton();
        botaoCadastrar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Cadastrar Atleta");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameActivated(evt);
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        labelMatricula.setText("Número de Matrícula");

        campoMatricula.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                campoMatriculaCaretUpdate(evt);
            }
        });

        labelRG.setText("RG");

        campoRG.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                campoRGCaretUpdate(evt);
            }
        });

        labelGenero.setText("Gênero");

        buttonGroup1.add(radioMasculino);
        radioMasculino.setText("Masculino");

        buttonGroup1.add(radioFeminino);
        radioFeminino.setText("Feminino");

        labelCurso.setText("Curso");

        campoNome.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                campoNomeCaretUpdate(evt);
            }
        });

        labelNome.setText("Nome Completo");

        javax.swing.GroupLayout abaDadosLayout = new javax.swing.GroupLayout(abaDados);
        abaDados.setLayout(abaDadosLayout);
        abaDadosLayout.setHorizontalGroup(
            abaDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(abaDadosLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(abaDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelGenero)
                    .addComponent(labelCurso)
                    .addComponent(labelMatricula)
                    .addComponent(labelNome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(abaDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(abaDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(seletorCurso, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, abaDadosLayout.createSequentialGroup()
                            .addGroup(abaDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(abaDadosLayout.createSequentialGroup()
                                    .addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(labelRG))
                                .addGroup(abaDadosLayout.createSequentialGroup()
                                    .addComponent(radioMasculino)
                                    .addGap(18, 18, 18)
                                    .addComponent(radioFeminino)))
                            .addGap(18, 18, 18)
                            .addComponent(campoRG, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(17, 17, 17))
        );
        abaDadosLayout.setVerticalGroup(
            abaDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(abaDadosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(abaDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelNome))
                .addGap(18, 18, 18)
                .addGroup(abaDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelRG)
                    .addComponent(campoRG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelMatricula))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(abaDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radioMasculino)
                    .addComponent(radioFeminino)
                    .addComponent(labelGenero))
                .addGap(18, 18, 18)
                .addGroup(abaDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(seletorCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCurso))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        painelAbas.addTab("Dados do Atleta", abaDados);

        scrollTabelaTimes.setPreferredSize(new java.awt.Dimension(100, 100));

        ArrayList<Integer> listaDeTimes;
        if (editAtleta == null){
            listaDeTimes = new ArrayList<Integer>(0);
        } else {
            listaDeTimes = editAtleta.getListaDeTimes();
        }
        tabelaTimes.setAutoCreateRowSorter(true);
        tabelaTimes.setBackground(new java.awt.Color(254, 254, 254));
        tabelaTimes.setModel(modeloTabelaTimes(listaDeTimes));
        scrollTabelaTimes.setViewportView(tabelaTimes);

        botaoGerenciar.setText("Gerenciar Time");
        botaoGerenciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoGerenciarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout abaTimesLayout = new javax.swing.GroupLayout(abaTimes);
        abaTimes.setLayout(abaTimesLayout);
        abaTimesLayout.setHorizontalGroup(
            abaTimesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(abaTimesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollTabelaTimes, javax.swing.GroupLayout.PREFERRED_SIZE, 410, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoGerenciar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        abaTimesLayout.setVerticalGroup(
            abaTimesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, abaTimesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(abaTimesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(botaoGerenciar)
                    .addComponent(scrollTabelaTimes, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))
                .addContainerGap())
        );

        painelAbas.addTab("Times do Atleta", abaTimes);

        botaoAlterar.setText("Alterar");
        botaoAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoAlterarActionPerformed(evt);
            }
        });

        botaoExcluir.setText("Excluir");
        botaoExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoExcluirActionPerformed(evt);
            }
        });

        botaoCancelar.setText("Cancelar");
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        botaoCadastrar.setText("Cadastrar");
        botaoCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCadastrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(93, Short.MAX_VALUE)
                .addComponent(botaoCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(82, 82, 82))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelAbas, javax.swing.GroupLayout.PREFERRED_SIZE, 571, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelAbas, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoCadastrar)
                    .addComponent(botaoAlterar)
                    .addComponent(botaoExcluir)
                    .addComponent(botaoCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
     this.dispose();
    }//GEN-LAST:event_botaoCancelarActionPerformed

    private void campoNomeCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_campoNomeCaretUpdate
          String text = campoNome.getText();
          int max = 49;
          if (text.length() > max) {
            text = text.substring (0, max);
              campoNome.setText (text);
          }
    }//GEN-LAST:event_campoNomeCaretUpdate

    private void campoMatriculaCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_campoMatriculaCaretUpdate
          String text = campoMatricula.getText();
          int max = 19;
          if (text.length() > max) {
            text = text.substring (0, max);
              campoMatricula.setText (text);
          }
    }//GEN-LAST:event_campoMatriculaCaretUpdate

    private void campoRGCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_campoRGCaretUpdate
          String text = campoRG.getText();
          int max = 19;
          if (text.length() > max) {
            text = text.substring (0, max);
              campoRG.setText (text);
          }
    }//GEN-LAST:event_campoRGCaretUpdate

    private void botaoCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCadastrarActionPerformed
        try {
            Atleta atleta = verificaDados();
            //insere no banco e mostra mensagem de dialogo
            if (banco.insereAtleta(atleta) > 0){
                JOptionPane.showMessageDialog(this, "Atleta cadastrado com sucesso!", "Cadastrar Atleta", JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
            }
        } catch (GUILudi ex) {
            Logger.getLogger(CriarAtleta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CriarModalidade.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_botaoCadastrarActionPerformed

    private void botaoAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAlterarActionPerformed
        try {
            Atleta atleta = verificaDados();
            atleta.setId(editAtleta.getId());
            atleta.setListaDeTimes(banco.getAtleta(editAtleta.getId()).getListaDeTimes());
            
            banco.setAtleta(atleta);
            JOptionPane.showMessageDialog(this, "Atleta alterado com sucesso!", "Cadastrar Atleta", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();

        } catch (SQLException ex) {
            Logger.getLogger(CriarAtleta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (GUILudi ex) {
            Logger.getLogger(CriarAtleta.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_botaoAlterarActionPerformed

    private void botaoExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoExcluirActionPerformed
        try {
            banco.deleteAtleta(editAtleta);
            JOptionPane.showMessageDialog(this, "Atleta excluído com sucesso!", "Cadastrar Atleta", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        } catch (SQLException ex) {
            Logger.getLogger(CriarAtleta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AtletaExcecao e) {
            if (e.getLocalizedMessage().equals("Atleta ainda esta em algum time")){
		JOptionPane.showMessageDialog(this, "Este Atleta ainda está em algum time.\nExclua-o destes times antes de excluí-lo!", "Cadastrar Atleta", JOptionPane.ERROR_MESSAGE);
	    }
            e.printStackTrace();
        }
    }//GEN-LAST:event_botaoExcluirActionPerformed

    private void formInternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameActivated
        try {

            if (editAtleta != null){
                editAtleta.setListaDeTimes(banco.getAtleta(editAtleta.getId()).getListaDeTimes());
            }
            updateTabelaTimes();
        } catch (SQLException ex) {
            Logger.getLogger(CriarAtleta.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formInternalFrameActivated

    private void botaoGerenciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoGerenciarActionPerformed
        if (tabelaTimes.getSelectedRow() < 0){
            JOptionPane.showMessageDialog(this, "Selecione um time!", "Cadastrar Atleta", JOptionPane.INFORMATION_MESSAGE);
        } else {
            String coluna;
            int i = -1;
            do {
                i++;
                coluna = tabelaTimes.getColumnName(i);
            } while (!(coluna.equals("ID")));

           CriarTime frame;
            try {
                frame = new CriarTime(banco, usuario, banco.getTime((Integer) tabelaTimes.getValueAt(tabelaTimes.getSelectedRow(), i)));
                frame.setVisible(true);
                this.getDesktopPane().add(frame);
                try {
                    frame.setSelected(true);
                } catch (java.beans.PropertyVetoException e) {}
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_botaoGerenciarActionPerformed

    private Atleta verificaDados() throws GUILudi, SQLException{

        if (campoNome.getText().equals("")){
            JOptionPane.showMessageDialog(this, "Nome não pode ficar em branco!", "Cadastrar Atleta", JOptionPane.ERROR_MESSAGE);
            throw new GUILudi("Dados invalidos!");
        }
        if (campoMatricula.getText().equals("")){
            JOptionPane.showMessageDialog(this, "Matrícula não pode ficar em branco!", "Cadastrar Atleta", JOptionPane.ERROR_MESSAGE);
            throw new GUILudi("Dados invalidos!");
        }
        if (campoRG.getText().equals("")){
            JOptionPane.showMessageDialog(this, "RG não pode ficar em branco!", "Cadastrar Atleta", JOptionPane.ERROR_MESSAGE);
            throw new GUILudi("Dados invalidos!");
        }
        if (editAtleta != null){
            editAtleta.setListaDeTimes(banco.getAtleta(editAtleta.getId()).getListaDeTimes());
            if (editAtleta.getListaDeTimes().size() > 0){
                System.out.println("te");
                if (!editAtleta.getCurso().equals(listaDeCursos.get(seletorCurso.getSelectedIndex()))){
                    JOptionPane.showMessageDialog(this, "Atleta está em um time!\nAntes de mudar o curso dele, exclua-o desse(s) time(s)!", "Cadastrar Atleta", JOptionPane.ERROR_MESSAGE);
                    throw new GUILudi("Dados invalidos!");
                }
            }
        }
        
        int genero = radioMasculino.isSelected() ? 1 : 2;
        Atleta atleta = new Atleta(campoMatricula.getText(), campoRG.getText(), campoNome.getText(),
                                    genero, listaDeCursos.get(seletorCurso.getSelectedIndex()));

        return atleta;
    }                                       

    private DefaultTableModel modeloTabelaTimes(ArrayList<Integer> listaDeTimes){
        String[][] dados = new String [0][];
        String[] colunas = new String [] {"ID", "Modalidade", "Gênero", "Curso"};

        DefaultTableModel modelo = new DefaultTableModel(dados, colunas){
             Class[] types = new Class [] {
                 java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
             };
             boolean[] canEdit = new boolean [] {
                 false, false, false, false
             };

             @Override
             public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
             }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        };
        
        for (int i = 0; i < listaDeTimes.size(); i++){
            try {
                Time time = banco.getTime(listaDeTimes.get(i));
                Modalidade modalidade = banco.getModalidade(time.getIdModalidade());
                String genero = (modalidade.getGenero() == 1)? "Masculino" : "Feminino";
                
                modelo.addRow(new Object [] {time.getId(), modalidade.getNome(), genero, time.getCurso()});
            } catch (SQLException ex) {
                Logger.getLogger(ConsultarTime.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return modelo;
    }

    private void updateTabelaTimes(){
        ArrayList<Integer> listaDeTimes;
        
        if (editAtleta == null){
            listaDeTimes = new ArrayList<Integer>(0);
        } else{
            listaDeTimes = editAtleta.getListaDeTimes();
        }
        
        tabelaTimes.setModel(modeloTabelaTimes(listaDeTimes));

        tabelaTimes.getColumnModel().getColumn(0).setMinWidth(40);
        tabelaTimes.getColumnModel().getColumn(0).setMaxWidth(40);
        tabelaTimes.getColumnModel().getColumn(1).setMinWidth(194);
        tabelaTimes.getColumnModel().getColumn(2).setMinWidth(100);
        tabelaTimes.getColumnModel().getColumn(2).setMaxWidth(100);
        tabelaTimes.getColumnModel().getColumn(3).setMinWidth(70);
        tabelaTimes.getColumnModel().getColumn(3).setMaxWidth(70);
        tabelaTimes.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel abaDados;
    private javax.swing.JPanel abaTimes;
    private javax.swing.JButton botaoAlterar;
    private javax.swing.JButton botaoCadastrar;
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoExcluir;
    private javax.swing.JButton botaoGerenciar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField campoMatricula;
    private javax.swing.JTextField campoNome;
    private javax.swing.JTextField campoRG;
    private javax.swing.JLabel labelCurso;
    private javax.swing.JLabel labelGenero;
    private javax.swing.JLabel labelMatricula;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelRG;
    private javax.swing.JTabbedPane painelAbas;
    private javax.swing.JRadioButton radioFeminino;
    private javax.swing.JRadioButton radioMasculino;
    private javax.swing.JScrollPane scrollTabelaTimes;
    private javax.swing.JComboBox seletorCurso;
    private javax.swing.JTable tabelaTimes;
    // End of variables declaration//GEN-END:variables
}
